package com.jiufeng.materialdesigndemo;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener {

    private ViewPager mPager;
    private TabLayout mTabLayout;
    private Toolbar mToolbar;
    private NavigationView mNavigationView;
    private DrawerLayout mDrawerLayout;
    private Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    private void initData() {
        List<String> mList=new ArrayList<>();
        for (int i=0;i<3;i++){
            mList.add(String.format(Locale.CHINA,"第%02d页",i));
        }
        mPager.setAdapter(new MyAdatper(getSupportFragmentManager(),mList));
        mTabLayout.setupWithViewPager(mPager);
    }

    private void initView() {
        mPager = (ViewPager) findViewById(R.id.pager);
        mTabLayout = (TabLayout) findViewById(R.id.tab);
        mToolbar=findViewById(R.id.toolbar);
        mNavigationView=findViewById(R.id.nav);
        mDrawerLayout=findViewById(R.id.drwaer);
        //以下步骤顺序很重要，set，get，new，add，sync
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        ActionBarDrawerToggle drawerToggle=new ActionBarDrawerToggle(this,mDrawerLayout,mToolbar,0,0);
        mDrawerLayout.addDrawerListener(drawerToggle);
        drawerToggle.syncState();

        mNavigationView=findViewById(R.id.nav);
        mNavigationView.setNavigationItemSelectedListener(this);
        mButton=findViewById(R.id.btn_delete);
        mButton.setOnClickListener(this);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.item_1:
                break;
            case R.id.item_4:
                finish();
                break;
        }
        mDrawerLayout.closeDrawer(Gravity.START);
        return true;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_delete:
                Snackbar.make(view,"确定删除吗?",Snackbar.LENGTH_SHORT)
                       // .setAction("撤销", new View.OnClickListener() {
                        .setAction("跳转", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                //Toast.makeText(MainActivity.this,"已经撤销删除",Toast.LENGTH_SHORT).show();
                                startActivity(new Intent(MainActivity.this,CoordinatorActivity.class));
                            }
                        })
                        .show();
                break;
        }
    }
}
